package com.worldcup.conf.beetl;

import com.ibeetl.starter.BeetlTemplateConfig;
import com.ibeetl.starter.BeetlTemplateCustomize;
import org.beetl.core.resource.ClasspathResourceLoader;
import org.beetl.ext.spring.BeetlGroupUtilConfiguration;
import org.beetl.ext.spring.BeetlSpringViewResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;

import java.util.Properties;

/**
 * @author duwan.zq
 * @date 2018/12/05
 */
@Configuration("beetlConfiguration")
public class BeetlConfig {

    @Value("${beetl.templatesPath:templates}")
    String templatesPath;

    @Autowired
    BeetlProperties beetlProperties;

    BeetlTemplateCustomize beetlTemplateCustomize;

    @Bean("beetlConfig")
    public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() {
        BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlConfiguration();

        try {
            ClassLoader loader = Thread.currentThread().getContextClassLoader();
            if (loader == null) {
                loader = BeetlTemplateConfig.class.getClassLoader();
            }

            beetlGroupUtilConfiguration.setConfigProperties(beetlProperties.getProperties());
            ClasspathResourceLoader cploder = new ClasspathResourceLoader(loader, this.templatesPath);
            beetlGroupUtilConfiguration.setResourceLoader(cploder);
            beetlGroupUtilConfiguration.init();
            beetlGroupUtilConfiguration.getGroupTemplate().setClassLoader(loader);
            if (this.beetlTemplateCustomize != null) {
                this.beetlTemplateCustomize.customize(beetlGroupUtilConfiguration.getGroupTemplate());
            }

            return beetlGroupUtilConfiguration;
        } catch (Exception var5) {
            throw new RuntimeException(var5);
        }
    }

}
